﻿using System;
using System.Web.UI;

using DeployWebLibUtils;

using WebControlsLibrary;
using WebControlsLibrary.Controls;
using WebControlsLibrary.Controls.UCs;

namespace WebApplicationTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (IsPostBack && LoadedControlType != null)
            {
                LoadUserControlOnPostBack();
            }
        }

        protected void btnRegisterUser_Click(object sender, EventArgs e)
        {
            LoadUserControl(typeof(RegisterUser));
        }

        protected void btnLoginPart_Click(object sender, EventArgs e)
        {
            LoadUserControl(typeof(LoginPart));
        }

        protected void btnProfile_Click(object sender, EventArgs e)
        {
            LoadUserControl(typeof(Profile));
        }

        private void LoadUserControl(Type controlType)
        {
            Type embeddControlType = controlType.GetWebEmbeddControlType();
            if (embeddControlType != null)
            {
                this.ph.Controls.Clear();
                InternalLoadUserControl(embeddControlType);
            }
        }

        private void InternalLoadUserControl(Type embeddControlType) 
        {
            Control control = Page.LoadControl(embeddControlType, null);
            if (control != null)
            {
                control.ID = "childControl";
                this.ph.Controls.Add(control);
                this.LoadedControlType = embeddControlType.AssemblyQualifiedName;
            }
        }

        private void LoadUserControlOnPostBack()
        {
            Type embeddControlType = Type.GetType(this.LoadedControlType, false);
            if (embeddControlType != null)
            {
                InternalLoadUserControl(embeddControlType);
            }
        }


        public string LoadedControlType
        {
            get
            {
                object o = Session["LoadedControlType"];
                if (o != null)
                {
                    return (string) o;
                }

                return null;
            }
            set
            {
                Session["LoadedControlType"] = value;
            }
        }
    }
}
